package dao

import (
	"context"
	"exam/6.11/model"
	"github.com/winksai/utils/pagination"
	"gorm.io/gorm"
)

type OtherDao struct {
	db *gorm.DB
}

func (o *OtherDao) Find(ctx context.Context, name string, Contacts string, Area string, createBy int, page int, pageSize int) []model.Supplier {
	var supplier []model.Supplier

	pagination := pagination.Pagination{
		Page:     page,
		PageSize: pageSize,
	}
	query := o.db
	if name != "" {
		query = query.Where("name LIKE ?", "%"+name+"%")
	}

	if Contacts != "" {
		query = query.Where("contacts = ?", Contacts)
	}
	if Area != "" {
		query = query.Where("area = ?", Area)
	}
	if createBy != 0 {
		query = query.Where("create_by = ?", createBy)
	}
	if page != 0 && pageSize != 0 {
		query = query.Offset(pagination.Offset()).Limit(pagination.Limit())
	}
	query.Select("name,level,contacts,email,phone,position,area,address,create_by").Order("created_at DESC").Find(&supplier)
	return supplier
}
func (o *OtherDao) Add(product model.Product) error {
	return o.db.Create(&product).Error
}

func NewOtherDao(db *gorm.DB) *OtherDao {
	return &OtherDao{db: db}
}
